from queue import Queue
from threading import Thread


class CmdExecutor:
    def __init__(self, func):
        self.cmd_queue = Queue()
        self.func = func
        self.__start()

    def __execute(self):
        while True:
            cmd_list = self.cmd_queue.get()
            try:
                self.func(cmd_list)
            except Exception as e:
                print(f"[DLS][ERROR] 命令执行失败{e}")

    def submit(self, cmd_list: list):
        self.cmd_queue.put(cmd_list)

    def __start(self):
        for _ in range(10):
            th = Thread(target=self.__execute, daemon=True)
            th.start()
